var hotpageno=1,maylikepage=1;
var hotpagecount=1,maylikepagecount=1;
$(function(){
	$('#J_slider').play({picList:".slider-main",navList:".slider-nav"});
	
	$("#J_news_tab .news-hd-main li").tab({tabBd : "#J_news_tab .item-bd"});
	$("#J_likePro_tab .likePro-hd-main li").tab({tabBd : "#J_likePro_tab .likePro-bd-main",fun:function(i){
		loadhotlikedata(i);
	}});
	
	loadfloors();
	
	$(document).keydown(function(event){if (event.keyCode == 13){keyenterto();}});
})

function keyenterto(){
	if(curinutfocus=='search')searchProduct();
	else if(curinutfocus=='login')tologin();
}

//加载首页楼层信息
function loadfloors(){
	$w.loadPanel(apiroot+"/index/floors",{},"floors");
}
function loadhotlikedata(n){
	if(n==0)loadhots(hotpageno);
	else loadhomeMaylike(maylikepage);
}
function loadhomeMaylike(pno){
	loadMaylike(5,loadMaylikeok,pno);
}
function loadhots(pno){
	var url=apiroot+'/product/hots?pagesize=5';
	if(pno)url+="&pageno="+pno;
	$w.httpRequest({
		 url:url,
		 data:{},
		 ok:function(ret){
			 loadhotsok(ret.Data);
		 }
	 })
}

function getcheckcodeimg(){
	$("#checkcodeimg").attr("src",server+"/validateimg?d="+new Date().getTime());
}

function tologin(){
$(".loginmsg").removeClass("loginmsgon");
$(".loginmsg").text("");
if($("[name='loginname']").val()==''){
	$(".loginmsg").addClass("loginmsgon");
	$(".loginmsg").text("请输入手机号/用户名！");
	$("[name='loginname']").focus();
}else if($("[name='pwd']").val()==''){
	$(".loginmsg").addClass("loginmsgon");
	$(".loginmsg").text("请输入登录密码！");
	$("[name='pwd']").focus();
}else{
    saveUserInfo();
	$(".login-btn").html('<img src="'+ctx+'/website/images/rr.gif" style="margin-top:9px">');
	$w.httpRequest({
		url:apiroot+"/user/login",
		data:{"loginname":$("[name='loginname']").val(),"pwd":$("[name='pwd']").val(),"checkcode":$("[name='checkcode']").val()},
		ok:function(ret){
			var user=ret.Data;
			$w.setLoginUser(user.userid,user.title,user.phone);
			window.location.reload();
			//if(sessionStorage.getItem("_curUrl_"))window.location.href=sessionStorage.getItem("_curUrl_");
			//else window.location.href=ctx;
		},fail:function(ret){
			$(".loginmsg").addClass("loginmsgon");
			$(".loginmsg").text(ret.MsgDesc);
			$(".login-btn").html('登录');
			return false;
		}
	})
}
}

function moreinfolist(){
	window.open(ctx+"/info/showlist?channelcode="+$(".news-hd-main li.cur").attr("code"));
}

function loadhotmay_prev(){
	var type=$(".likePro-hd-main li.cur").attr("type");
	if(type=='maylike'){
		if(maylikepage>1){
			maylikepage--;loadhomeMaylike(maylikepage);
		}
	}else {
		if(hotpageno>1){
			hotpageno--;loadhots(hotpageno);
		}
	}
}
function loadhotmay_next(){
	var type=$(".likePro-hd-main li.cur").attr("type");
	if(type=='maylike'){
		if(maylikepage<maylikepagecount){
			maylikepage++;loadhomeMaylike(maylikepage);
		}
	}
	else{
		if(hotpageno<hotpagecount){
			hotpageno++;loadhots(hotpageno);
		}
	}
}
function loadhotsok(datas){
	var panel=$("#hotpush");
	panel.html('');
	hotpagecount=datas.pcount;
	checkshowbtn();
  for(var i=0;i<datas.list.length;i++){
	  var data=datas.list[i];
	  var h='<li class="likePro-bd-item">'
	      	+'<div class="likePro-bd-pic"><a href="'+ctx+'/product/info?priceid='+data.priceid+'" target="_blank"><img src="'+getRandomFileserver()+data.photo+'" class="img-bg-loading" style="width:220px;height:220px;" onerror="this.src=\''+ctx+'/website/images/noimg.jpg\'"/></a></div>'
	      	+'<a href="'+ctx+'/product/info?priceid='+data.priceid+'" target="_blank" class="likePro-bd-info">'
	      	+'	<p class="title">'+ data.productname + ' ' +(data.b_guige?data.b_guige:"") +'</p>'
	      	// +'	<p class="col999" style="font-size:12px;">'+(data.b_pzwh?data.b_pzwh:"")+'</p>'
	      	+'	<p class="col999" style="font-size:12px;width: 220px;overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">'+(data.b_chanjia?data.b_chanjia:"")+'</p>'
	      	+'	<p class="cost">'+((isuserauth && islogin)?'¥'+data.price:(!islogin?"<font class='red'>登录可见</font>":"<font class='red'>审核通过可见</font>"))+'</p>'
	      	+'</a>'
	      	+'</li>';
	  panel.append(h);
  }
}
function loadMaylikeok(datas){
	var panel=$("#maylike");
	panel.html('');
	maylikepagecount=datas.pcount;
	checkshowbtn('maylike');
	for(var i=0;i<datas.list.length;i++){
	  var data=datas.list[i];
	  var cjia=(data.b_chanjia?data.b_chanjia:"");
	  if(cjia.length>15)cjia=cjia.substr(0,15)+"...";
	  var h='<li class="likePro-bd-item">'
	      	+'<div class="likePro-bd-pic"><a href="'+ctx+'/product/info?priceid='+data.id+'" target="_blank"><img src="'+getRandomFileserver()+data.photo+'" class="img-bg-loading" style="width:220px;height:220px;" onerror="this.src=\''+ctx+'/website/images/noimg.jpg\'"/></a></div>'
	      	+'<a href="'+ctx+'/product/info?priceid='+data.id+'" target="_blank" class="likePro-bd-info">'
	      	+'	<p class="title">'+ data.productname +' ' + (data.b_guige?data.b_guige:"") +'</p>'
	      	// +'	<p class="col999" style="font-size:12px;">'+(data.b_pzwh?data.b_guige:"")+'</p>'
	      	+'	<p class="col999" style="font-size:12px;width: 220px;overflow: hidden; white-space: nowrap; text-overflow: ellipsis;">'+cjia+'</p>'
	      	+'	<p class="cost">'+((isuserauth && islogin)?'¥'+data.price:(!islogin?"<font class='red'>登录可见</font>":"<font class='red'>审核通过可见</font>"))+'</p>'
	      	+'</a>'
	      	+'</li>';
	  panel.append(h);
  }
}
function checkshowbtn(type){
	if(type=='maylike'){
		$("#likePro_p1").text(maylikepage);
		$("#likePro_p2").text(maylikepagecount);
		if(maylikepage>1){$(".btn-prev").addClass("btn-prev-cur");}else $(".btn-prev").removeClass("btn-prev-cur");
		if(maylikepage<maylikepagecount)$(".btn-next").addClass("btn-next-cur");else $(".btn-next").removeClass("btn-next-cur");
	}else{
		$("#likePro_p1").text(hotpageno);
		$("#likePro_p2").text(hotpagecount);
		if(hotpageno>1)$(".btn-prev").addClass("btn-prev-cur");else $(".btn-prev").removeClass("btn-prev-cur");
		if(hotpageno<hotpagecount)$(".btn-next").addClass("btn-next-cur");else $(".btn-next").removeClass("btn-prev-cur");
	}
}

function qunima(){
	window.location.href=ctx+"/coupon/show";
}
function qunimb(){
	window.location.href=ctx+"/user/integral";
}